package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service // 交给IOC容器, 管理
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param dto
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO dto) {
        //1.构造ShoppingCart对象
        ShoppingCart shoppingCart = new ShoppingCart();
        //1.1 属性拷贝
        BeanUtils.copyProperties(dto, shoppingCart); //浅拷贝

        //3.判断商品在购物车中是否存在 -- 条件: dishId+dishFlavor+userId
        //3.1查询当前用户自己的购物车
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart cart = shoppingCartMapper.selectBy(shoppingCart);

        //3.2判断商品是否在购物车中存在
        if (cart == null) {
            //2.补齐缺失属性
            //2.1判断是新增套餐还是新增菜品
            if (dto.getDishId() != null) { // 新增菜品
                //2.2根据菜品id,查询菜品信息
                Dish dish = dishMapper.selectById(dto.getDishId());
                //开始补充属性
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
               /* shoppingCart.setNumber(1);  //新增 ----> 到底是1还是+1? 判断商品在购物车中是否存在
                shoppingCart.setCreateTime(LocalDateTime.now());*/
            } else { //新增套餐
                //2.3根据套餐id, 查询套餐信息
                Setmeal setmeal = setmealMapper.selectSetmealById(dto.getSetmealId());
                //开始补充属性
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);  //新增 ----> 到底是1还是+1? 判断商品在购物车中是否存在
            shoppingCart.setCreateTime(LocalDateTime.now());
            //4.将商品数据存入到shopping_cart表中
            shoppingCartMapper.insert(shoppingCart);
        } else {  //代表购物车有该商品
            //4.将原来的购物车数量+1, 调用mapper层方法跟新
            //不用管是套餐还是菜品, 直接加一
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.update(cart);
        }
    }

    /**
     * 查询购物窗格
     *
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        //注意: 只能查询, 自己名下的购物车, 因此, 条件user_id
        return shoppingCartMapper.list(BaseContext.getCurrentId());
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        //注意: 只能查询, 自己名下的购物车, 因此, 条件user_id
        shoppingCartMapper.delete(BaseContext.getCurrentId());
    }

    /**
     * 删除购物车单个商品
     *
     * @param dto
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO dto) {
        //1.构造ShoppingCart对象
        ShoppingCart shoppingCart = new ShoppingCart();
        //1.1 拷贝属性
        BeanUtils.copyProperties(dto, shoppingCart);
        //1.2 补齐缺失属性 -- > 判断当前商品的数量是否为1
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart cart = shoppingCartMapper.selectBy(shoppingCart);
        //2.判断数量是否大于1 大于就减一
        if (cart.getNumber() > 1) {  //数量大于1
            //2.1将商品数量减一
            cart.setNumber(cart.getNumber() - 1);
            shoppingCartMapper.update(cart);
        } else { //数量等于 1
            //3.将商品从购物车中删除
            //判断是dish_id, 还是setmeal_id
            shoppingCartMapper.deletebyShoppingCart(cart);
        }
    }
}
